//
//  ComprehensiveDataViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/17.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

import Charts

import SnapKit


class ComprehensiveDataViewController: BaseViewController {
    
    let barChart = BarChartView.init()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        createChart()
        // Do any additional setup after loading the view.
    }
    
    func createChart() -> Void {
        
        self.view.addSubview(barChart)
        
        barChart.doubleTapToZoomEnabled = false
        
        barChart.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: self.view.bounds.size.width - 20, height: 300))
            make.center.equalTo(self.view)
        }
        
        barChart.legend.enabled = true
        
        barChart.noDataText = "暂无数据"
        
        barChart.drawValueAboveBarEnabled = true
        
        barChart.drawBarShadowEnabled = false
        
        let xAxis = self.barChart.xAxis
        
        xAxis.labelPosition = .bottom
        
        xAxis.drawGridLinesEnabled = false
        
        xAxis.labelTextColor = .hex("999999")
        
        self.barChart.rightAxis.enabled = false
        
        let leftAxis = self.barChart.leftAxis
        
        leftAxis.forceLabelsEnabled = false
        
        leftAxis.drawZeroLineEnabled = true
        
        leftAxis.axisLineWidth = 1
        
        leftAxis.labelCount = 5
        
        leftAxis.labelPosition = .outsideChart
        
        leftAxis.labelTextColor = .hex("999999")
        
        leftAxis.drawGridLinesEnabled = false
        
        barChart.legend.enabled = true
        
        barChart.data = self.setData()
        
//        barChart.legend.entries
//        chartView.delegate = self
//        
//        self.view.addSubview(chartView)
//        
//        var dataEntries: [BarChartDataEntry] = []
//        
//        for i in 0 ..< dataPoints.count {
//            
//            let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
//            
//            dataEntries.append(dataEntry)
//        }
//        
//        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
//        
//        let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
//        // 加上一个界限, 演示图中红色的线
//        let jx = ChartLimitLine(limit: 12.0, label: "I am LimitLine")
//        
//        chartView.rightAxis.addLimitLine(jx)
//        
//        chartView.data = chartData
//        // 自定义颜色
//        // 例子中有十二个柱状图
//        // colors 是一个数组, 可以给相应的颜色
//        chartDataSet.colors = [UIColor.blueColor(), UIColor.redColor(), UIColor.cyanColor()]
//        // API 自带颜色模板
//        // ChartColorTemplates.liberty()
//        // ChartColorTemplates.joyful()
//        // ChartColorTemplates.pastel()
//        // ChartColorTemplates.colorful()
//        // ChartColorTemplates.vordiplom()
//        chartDataSet.colors = ChartColorTemplates.liberty()
//        /**
//         // 动画效果, 简单列举几个, 具体请看API
//         case EaseInBack
//         case EaseOutBack
//         case EaseInOutBack
//         case EaseInBounce
//         case EaseOutBounce
//         case EaseInOutBounce
//         */
//        // 加上动画
//        chartView.animate(yAxisDuration: 1.0, easingOption: .EaseInBounce)
    }

    
    func setData() -> BarChartData {
        
        let xVals_count = 5
        
        let maxYVal = 500
        
//        var xVals: Array<String> = ["I2","GP","EC","P2","IC"]
        
        var yVals:Array<BarChartDataEntry> = []
        
        for i in 0 ..< xVals_count {
            
            let max = maxYVal + 1
            
            let val1 =  Double(arc4random_uniform(UInt32(max)))
            
            let val2 = Double(arc4random_uniform(UInt32(max)))
            
            let val3 = Double(arc4random_uniform(UInt32(max)))
            
            let entry = BarChartDataEntry.init(x: Double(i), yValues: [val1,val2,val3])
            
            yVals.append(entry)
        }
        
        let set1 = BarChartDataSet.init(values: yVals, label: nil)
        
        set1.highlightEnabled = true
        
        set1.colors = [.red,.blue,.yellow]
        
        var dataSets: Array<BarChartDataSet> = []
        
        dataSets.append(set1)
        
        let data = BarChartData.init(dataSets: dataSets)
        
//        let d = BarChartData.init(dataSets: [IChartDataSet])
        
//        let chartData = BarChartData(xVals: xVals, dataSet: dataSets)
        
        data.setValueFont(UIFont.systemFont(ofSize: 10))
        
        data.setValueTextColor(UIColor.orange)
        
//        let formatter = NumberFormatter()
        
//        formatter.numberStyle = .decimal
        
//        formatter.positiveFormat = ""
//        data.setValueFormatter(formatter)
        
        return data
        
//        var dataSets: Array<BarChartData>
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
